﻿/*
 * Jonathan
 * Created by: Jonathan Montiverdi
 * Date: 07/13/2012
 * License: http://jonathan.codeplex.com/license
 */
using System.Collections.Generic;
using System.Linq;

using Monti.AutoComplete;

namespace Jonathan.Data
{
   /// <summary>
   /// Description of SearchHistory.
   /// </summary>
   public class SearchHistory : IAutoCompleteDataProvider
   {      
      /// <summary>
      /// Initializes a new search history.
      /// </summary>
      public SearchHistory()
      {
         this.History = new List<string> ();
      }
      
      /// <summary>
      /// Gets the history.
      /// </summary>
      public List<string> History { get; private set; }
      
      /// <summary>
      /// Adds an item to the search history.
      /// </summary>
      /// <param name="item">Item to add.</param>
      public void AddItem (string item)
      {
         var itemLower = item.ToLower ();
         var isInHistory = this.History.Any (historyItem => historyItem.ToLower () == itemLower);
         if (!isInHistory)
            this.History.Add (item);
      }
      
      /// <summary>
      /// Gets a list of items to add to the autocomplete based on the give filter.
      /// </summary>
      /// <param name="filter">Filter to use.</param>
      /// <returns>List of auto complete items.</returns>
      public IEnumerable<string> GetAutoCompleteItems(string filter)
      {
         string lowerFilter = filter.ToLower ();
         return this.History.Where (item => item.ToLower ().Contains (lowerFilter));
      }
   }
}
